<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .login-container {
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            width: 350px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
        }

        input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }

        button {
            width: 100%;
            padding: 12px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }

        button:hover {
            background-color: #45a049;
        }
    </style>
</head>

<body>
    <div class="login-container">
        <h2>用户登录</h2>
        <form id="loginForm">
            <div class="form-group">
                <label for="desPassword">账户密码</label>
                <input type="password" id="desPassword" name="desPassword" required>
            </div>
            <div class="form-group">
                <label for="loginName">用户账户</label>
                <input type="text" id="loginName" name="loginName" required>
            </div>
            <div class="form-group">
                <label for="password">用户登录密码</label>
                <input type="password" id="password" name="password" required>
            </div>
            <button type="submit">登录</button>
        </form>
    </div>

    
</body>
<script src="/js/mqtt.js"></script>
<script>
        document.getElementById('loginForm').addEventListener('submit', function (e) {
            e.preventDefault();

            const formData = {
                desPassword: document.getElementById('desPassword').value,
                loginName: document.getElementById('loginName').value,
                password: document.getElementById('password').value
            };

            fetch('/security/account/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify(formData)
            })
                .then(response => response.json())
                .then(data => {
                    if (data.flag == 1) {
                        localStorage.setItem('token', data.data);
                        setMqttStatus("false");
                        window.location.href = '/index.html'; 
                    }
                    else {
                        alert('登录失败: ' + (data.message || '未知错误'));
                    }

                })
                .catch(error => {
                    console.error('Error:', error);
                    alert('登录请求失败');
                });
        });
    </script>
</html>